﻿#include "dialog.h"
#include "ui_dialog.h"

#include <QTime>
#include <QTimer>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QTextCodec>

#include <QFileDialog>
#include <QDebug>




Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);

    timer1 = new QTimer(this);//设置定时器
    connect(timer1,SIGNAL(timeout()),this,SLOT(onRangeChanged()));//计时器绑定

    m_iniFile = "luckySet.ini";
    m_loadFile = ".csv";


    LoadSettings();
    ui->LineEdit_list->setText(m_loadFile);
    winner=-1;


}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::onRangeChanged()
{
    int num = m_listPerson.size();//人员名单个数
    if(num == 0)//全部都中奖后重新加载名单开启第二轮抽奖
    {
        GetListFromFile(m_loadFile);
        num = m_listPerson.size();
    }

    winner = qrand()%num;
    qDebug() << winner;

    m_strWinner = m_listPerson[winner].name;
    ui->label_show->setText(m_strWinner);//显示姓名
}



void Dialog::GetListFromFile(const QString &file)//只读，读取人员名单
{

    QFile csvFile(file);
    if(!csvFile.open(QIODevice::Text | QIODevice::ReadOnly))
    {
        QMessageBox::warning(this,tr("WARNING"),tr("OPEN FILE ERROR"));
        return ;
    }
    QTextStream textStream(&csvFile);
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    textStream.setCodec(codec);
    QString strHeader = textStream.readLine();
    QString strLine = textStream.readLine();
    T_ListofPerson person;
    while (!strLine.isEmpty()) {

        QStringList strList = strLine.split(",");
        person.number = strList.at(0);
        person.name = strList.at(1);


        m_listPerson.push_back(person);
        strLine = textStream.readLine();
    }
    csvFile.close();
}

void Dialog::SaveSettings()
{
    QSettings setting(m_iniFile,QSettings::IniFormat);
    setting.setValue("path/path",m_loadFile);
}

void Dialog::LoadSettings()
{
    QSettings setting(m_iniFile,QSettings::IniFormat);
    m_loadFile = setting.value("path/path").toString();
}

void Dialog::SelectWinner()
{
    if(("停止") == ui->label_run->text())
    {
        ui->label_run->setText("开始");//按钮改为“开始”
        timer1->stop();//定时器停止滚动
        m_listPerson.remove(winner);//将中奖者移除名单
    }
    else
    {
        GetListFromFile(m_loadFile);//读取人员名单，用到自己定义的 【GetListFromFile】 函数
        ui->label_run->setText("停止");//按钮改为“停止”
        timer1->setInterval(50);//变换的频率
        qsrand((uint)(QTime::currentTime().msec()));
        qDebug() << "开始";
        timer1->start();//定时器开始滚动
    }
    ui->label_show->setText(m_strWinner);//显示人名
}

void Dialog::on_pushButton_openFile_clicked()
{
    m_loadFile = QFileDialog::getOpenFileName(this,tr("选择文件"),QDir::currentPath(),QString("(*.txt *.csv)"));
    if(m_loadFile.isEmpty())
    {
        QMessageBox::warning(this,tr("WARNING"),tr("NOT SELECT FILE"));
        return;
    }
    ui->LineEdit_list->setText(m_loadFile);
    SaveSettings();
}

void Dialog::on_LineEdit_list_editingFinished()
{
    m_loadFile = ui->LineEdit_list->text();
    SaveSettings();
}

void Dialog::keyPressEvent(QKeyEvent *event)//键盘操作
{
    if(event->modifiers() == Qt::ControlModifier)//点击ctrl键
    {
        if(event->key() == Qt::Key_S)//点击s键
        {
            SelectWinner();
        }
    }
}

void Dialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)//单击左键
    {
        SelectWinner();
    }
}


